<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Repository extends Model
{
    private $timings;

    protected $events = [
        'updating' => Events\RepositoryUpdating::class,
    ];
    protected $fillable = ['name', 'type', 'category', 'upstream_url', 'description'];

    public function virtuals()
    {
        return $this->belongsToMany('App\Repository', 'virtual_repositories', 'parent_repo', 'child_repo');
    }

    public function rules()
    {
        return $this->hasMany('App\Rule');
    }

    public function timingsStart()
    {
        $this->timings = microtime(true);
    }

    public function timingsStop()
    {
        $this->delay = intval(round((microtime(true) - $this->timings) * 1000));
        $this->update();
    }
}
